package com.jdbc_;

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class Jdbc2 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        // 1、注册驱动（利用反射加载Driver类）
        Class<?> clz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clz.newInstance();

        // 2、获取连接
        String url = "jdbc:mysql://localhost:3306/hsp2";
        Properties properties = new Properties();
        properties.setProperty("user", "root");  // 用户，user格式是规定好的
        properties.setProperty("password", "lxl");  // 密码，password格式是规定好的

        Connection connect = driver.connect(url, properties);  // 这里真正获取到连接对象

        // 3、执行增删改查
        String sql = "insert into actor values(null, '刘德华', 'M', '1970-1-1', '12345678')";
        // Statement对象用于执行SQL语句
        Statement statement = connect.createStatement();
        int rows = statement.executeUpdate(sql);  // 如果是增删改语句，返回结果就是Affected rows

        System.out.println(rows > 0 ? "成功" : "失败");

        // 4、依次关闭连接，释放资源
        statement.close();
        connect.close();
    }
}